/**
 * Created by Administrator on 2017/11/23.
 */
import {newExamineDistributor, queryDistributor} from '../services/distributorApi'
import {examineDistributor} from '../services/distributorApi';
import {examineSupplier} from '../services/distributorApi';
import {queryPassAndUnPass} from '../services/distributorApi'
export  default {
    namespace:'distributor',
    state:{
        loading: true,
        Data: {
            list: [],
            pagination: {},
        },
        passAndUnPass:{
            list: [],
            pagination: {},
        },
        amount:[],
        id:'',
        selectStatus:'',
    },
    effects:{
        *queryDistributor({payload},{call,put}){
            yield  put({
                type:'handleLoading',
                payload:true,
            });
           const response=yield call(queryDistributor,payload);
           yield put({
               type:'handleResponse',
               payload:response,
           });
           yield put({
               type:'handleLoading',
               payload:false,
           });
        },
        *queryPassAndUnPass({payload},{call,put}){
            yield  put({
                type:'handleLoading',
                payload:true,
            });
            const response=yield call(queryPassAndUnPass,payload);
            yield put({
                type:'handlePass',
                payload:response,
            });
            yield put({
                type:'handleLoading',
                payload:false,
            });
        },
        *querySupplier({payload},{call,put}){
            yield  put({
                type:'handleLoading',
                payload:true,
            });
            const response=yield call(queryDistributor,payload);
            yield put({
                type:'handleSupplier',
                payload:response,
            });
            yield put({
                type:'handleLoading',
                payload:false,
            });
        },
        *updateDistributor({payload,callback},{call,put}){
            const response=yield call(examineDistributor,payload);
            yield put({
                type:'handleUpdate',
                payload:response,
            });
            yield put({
                type:'changeId',
                payload:payload.id,
                });
            if(callback)callback(response);
        },
        * keepData({payload, callback},{put,call}){
            yield put({
                type:'saveFormData',
                payload:true,
            });
            yield put({
                type:'saveFormData',
                payload:payload,
            });
            if (callback) callback(true);
        },

        *newUpdateDistributor({payload,callback},{call,put}){
            const response=yield call(newExamineDistributor,payload);
            yield put({
                type:'handleUpdate',
                payload:response,
            });
            yield put({
                type:'changeId',
                payload:payload.id,
            });
            if(callback)callback(response);
        },
        *updateSupplier({payload},{call,put}){
          const  response =yield call(examineSupplier,payload)
        },
    },
    reducers:{
        saveFormData(state, { payload }) {
            return {
                ...state,

                step: {
                    ...state.step,
                    ...payload,
                },
                loading:payload
            };
        },
        handleSupplier(state,{payload}){
            return ({
                ...state,
                Data:payload.resultData.allDistributor,
                amount:payload.resultData.amount,
            })
        },
        handleResponse(state,{payload}){
            return({
                ...state,
                Data:payload.resultData.allDistributor,
                amount:payload.resultData.amount,
            })
        },
        handleUpdate(state,{payload}){
            return({
                ...state,
                resultInfo:payload["result"],
            })
        },
        handleLoading(state,{payload}){
            return ({
                ...state,
                loading:payload,
            })
        },
        handlePass(state,{payload}){
            return ({
                ...state,
                passAndUnPass:payload.resultData.passAndUnPass
            })
        },
        changeId(state,{payload}){
            return ({
                ...state,
                id:payload,
            })
        },
    }
}